<!DOCTYPE HTML>
<html lang="en" >
    
    <head>
        
        <meta charset="UTF-8">
        <meta http-equiv="X-UA-Compatible" content="IE=edge" />
        <title>ajax | 欢迎学习django课程</title>
        <meta content="text/html; charset=utf-8" http-equiv="Content-Type">
        <meta name="description" content="">
        <meta name="generator" content="GitBook 2.6.7">
        
        
        <meta name="HandheldFriendly" content="true"/>
        <meta name="viewport" content="width=device-width, initial-scale=1, user-scalable=no">
        <meta name="apple-mobile-web-app-capable" content="yes">
        <meta name="apple-mobile-web-app-status-bar-style" content="black">
        <link rel="apple-touch-icon-precomposed" sizes="152x152" href="../gitbook/images/apple-touch-icon-precomposed-152.png">
        <link rel="shortcut icon" href="../gitbook/images/favicon.ico" type="image/x-icon">
        
    <link rel="stylesheet" href="../gitbook/style.css">
    
        
        <link rel="stylesheet" href="../gitbook/plugins/gitbook-plugin-highlight/website.css">
        
    
        
        <link rel="stylesheet" href="../gitbook/plugins/gitbook-plugin-search/search.css">
        
    
        
        <link rel="stylesheet" href="../gitbook/plugins/gitbook-plugin-fontsettings/website.css">
        
    
    

        
    
    
    <link rel="next" href="../part5/8.html" />
    
    
    <link rel="prev" href="../part5/6.html" />
    

        
    </head>
    <body>
        
        
    <div class="book"
        data-level="5.6"
        data-chapter-title="ajax"
        data-filepath="part5/7.md"
        data-basepath=".."
        data-revision="Tue Jan 03 2017 08:43:08 GMT+0800 (CST)"
        data-innerlanguage="">
    

<div class="book-summary">
    <nav role="navigation">
        <ul class="summary">
            
            
            
            

            

            
    
        <li class="chapter " data-level="0" data-path="index.html">
            
                
                    <a href="../index.html">
                
                        <i class="fa fa-check"></i>
                        
                        课程介绍
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="1" data-path="part1/1.html">
            
                
                    <a href="../part1/1.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>1.</b>
                        
                        入门
                    </a>
            
            
            <ul class="articles">
                
    
        <li class="chapter " data-level="1.1" data-path="part1/2.html">
            
                
                    <a href="../part1/2.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>1.1.</b>
                        
                        搭建开发环境
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="1.2" data-path="part1/3.html">
            
                
                    <a href="../part1/3.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>1.2.</b>
                        
                        设计模型
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="1.3" data-path="part1/4.html">
            
                
                    <a href="../part1/4.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>1.3.</b>
                        
                        管理站点
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="1.4" data-path="part1/5.html">
            
                
                    <a href="../part1/5.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>1.4.</b>
                        
                        视图
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="1.5" data-path="part1/6.html">
            
                
                    <a href="../part1/6.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>1.5.</b>
                        
                        模板
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="1.6" data-path="part1/7.html">
            
                
                    <a href="../part1/7.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>1.6.</b>
                        
                        总结与作业
                    </a>
            
            
        </li>
    

            </ul>
            
        </li>
    
        <li class="chapter " data-level="2" data-path="part2/1.html">
            
                
                    <a href="../part2/1.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>2.</b>
                        
                        模型
                    </a>
            
            
            <ul class="articles">
                
    
        <li class="chapter " data-level="2.1" data-path="part2/2.html">
            
                
                    <a href="../part2/2.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>2.1.</b>
                        
                        定义模型
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="2.2" data-path="part2/3.html">
            
                
                    <a href="../part2/3.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>2.2.</b>
                        
                        模型成员
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="2.3" data-path="part2/4.html">
            
                
                    <a href="../part2/4.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>2.3.</b>
                        
                        模型查询
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="2.4" data-path="part2/5.html">
            
                
                    <a href="../part2/5.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>2.4.</b>
                        
                        自连接
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="2.5" data-path="part2/6.html">
            
                
                    <a href="../part2/6.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>2.5.</b>
                        
                        总结与作业
                    </a>
            
            
        </li>
    

            </ul>
            
        </li>
    
        <li class="chapter " data-level="3" data-path="part3/1.html">
            
                
                    <a href="../part3/1.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>3.</b>
                        
                        视图
                    </a>
            
            
            <ul class="articles">
                
    
        <li class="chapter " data-level="3.1" data-path="part3/2.html">
            
                
                    <a href="../part3/2.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>3.1.</b>
                        
                        URLconf
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="3.2" data-path="part3/3.html">
            
                
                    <a href="../part3/3.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>3.2.</b>
                        
                        视图函数
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="3.3" data-path="part3/4.html">
            
                
                    <a href="../part3/4.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>3.3.</b>
                        
                        Request对象
                    </a>
            
            
            <ul class="articles">
                
    
        <li class="chapter " data-level="3.3.1" data-path="part3/4_1.html">
            
                
                    <a href="../part3/4_1.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>3.3.1.</b>
                        
                        QueryDict对象
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="3.3.2" data-path="part3/4_2.html">
            
                
                    <a href="../part3/4_2.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>3.3.2.</b>
                        
                        GET属性
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="3.3.3" data-path="part3/4_3.html">
            
                
                    <a href="../part3/4_3.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>3.3.3.</b>
                        
                        POST属性
                    </a>
            
            
        </li>
    

            </ul>
            
        </li>
    
        <li class="chapter " data-level="3.4" data-path="part3/5.html">
            
                
                    <a href="../part3/5.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>3.4.</b>
                        
                        Response对象
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="3.5" data-path="part3/6.html">
            
                
                    <a href="../part3/6.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>3.5.</b>
                        
                        状态保持
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="3.6" data-path="part3/7.html">
            
                
                    <a href="../part3/7.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>3.6.</b>
                        
                        总结与作业
                    </a>
            
            
        </li>
    

            </ul>
            
        </li>
    
        <li class="chapter " data-level="4" data-path="part4/1.html">
            
                
                    <a href="../part4/1.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>4.</b>
                        
                        模板
                    </a>
            
            
            <ul class="articles">
                
    
        <li class="chapter " data-level="4.1" data-path="part4/2.html">
            
                
                    <a href="../part4/2.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>4.1.</b>
                        
                        定义模板
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="4.2" data-path="part4/3.html">
            
                
                    <a href="../part4/3.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>4.2.</b>
                        
                        模板继承
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="4.3" data-path="part4/4.html">
            
                
                    <a href="../part4/4.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>4.3.</b>
                        
                        HTML转义
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="4.4" data-path="part4/5.html">
            
                
                    <a href="../part4/5.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>4.4.</b>
                        
                        CSRF
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="4.5" data-path="part4/6.html">
            
                
                    <a href="../part4/6.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>4.5.</b>
                        
                        验证码
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="4.6" data-path="part4/7.html">
            
                
                    <a href="../part4/7.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>4.6.</b>
                        
                        总结与作业
                    </a>
            
            
        </li>
    

            </ul>
            
        </li>
    
        <li class="chapter " data-level="5" data-path="part5/1.html">
            
                
                    <a href="../part5/1.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>5.</b>
                        
                        高级
                    </a>
            
            
            <ul class="articles">
                
    
        <li class="chapter " data-level="5.1" data-path="part5/2.html">
            
                
                    <a href="../part5/2.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>5.1.</b>
                        
                        静态文件处理
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="5.2" data-path="part5/3.html">
            
                
                    <a href="../part5/3.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>5.2.</b>
                        
                        中间件
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="5.3" data-path="part5/4.html">
            
                
                    <a href="../part5/4.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>5.3.</b>
                        
                        上传图片
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="5.4" data-path="part5/5.html">
            
                
                    <a href="../part5/5.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>5.4.</b>
                        
                        Admin站点
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="5.5" data-path="part5/6.html">
            
                
                    <a href="../part5/6.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>5.5.</b>
                        
                        分页
                    </a>
            
            
        </li>
    
        <li class="chapter active" data-level="5.6" data-path="part5/7.html">
            
                
                    <a href="../part5/7.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>5.6.</b>
                        
                        ajax
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="5.7" data-path="part5/8.html">
            
                
                    <a href="../part5/8.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>5.7.</b>
                        
                        总结与作业
                    </a>
            
            
        </li>
    

            </ul>
            
        </li>
    
        <li class="chapter " data-level="6" data-path="part6/1.html">
            
                
                    <a href="../part6/1.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>6.</b>
                        
                        第三方
                    </a>
            
            
            <ul class="articles">
                
    
        <li class="chapter " data-level="6.1" data-path="part6/2.html">
            
                
                    <a href="../part6/2.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>6.1.</b>
                        
                        富文本编辑器
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="6.2" data-path="part6/3.html">
            
                
                    <a href="../part6/3.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>6.2.</b>
                        
                        缓存
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="6.3" data-path="part6/4.html">
            
                
                    <a href="../part6/4.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>6.3.</b>
                        
                        全文检索
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="6.4" data-path="part6/5.html">
            
                
                    <a href="../part6/5.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>6.4.</b>
                        
                        celery
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="6.5" data-path="part6/6.html">
            
                
                    <a href="../part6/6.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>6.5.</b>
                        
                        布署
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="6.6" data-path="part6/7.html">
            
                
                    <a href="../part6/7.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>6.6.</b>
                        
                        总结与作业
                    </a>
            
            
        </li>
    

            </ul>
            
        </li>
    
        <li class="chapter " data-level="7" data-path="part7/1.html">
            
                
                    <a href="../part7/1.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>7.</b>
                        
                        Git与项目
                    </a>
            
            
            <ul class="articles">
                
    
        <li class="chapter " data-level="7.1" data-path="part7/2.html">
            
                
                    <a href="../part7/2.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>7.1.</b>
                        
                        Git简介
                    </a>
            
            
            <ul class="articles">
                
    
        <li class="chapter " data-level="7.1.1" data-path="part7/2_2.html">
            
                
                    <a href="../part7/2_2.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>7.1.1.</b>
                        
                        远程仓库
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="7.1.2" data-path="part7/2_1.html">
            
                
                    <a href="../part7/2_1.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>7.1.2.</b>
                        
                        本地仓库
                    </a>
            
            
        </li>
    

            </ul>
            
        </li>
    
        <li class="chapter " data-level="7.2" data-path="part7/3.html">
            
                
                    <a href="../part7/3.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>7.2.</b>
                        
                        项目设计
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="7.3" data-path="part7/4.html">
            
                
                    <a href="../part7/4.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>7.3.</b>
                        
                        模型类设计
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="7.4" data-path="part7/5.html">
            
                
                    <a href="../part7/5.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>7.4.</b>
                        
                        使用页面
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="7.5" data-path="part7/6.html">
            
                
                    <a href="../part7/6.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>7.5.</b>
                        
                        总结与作业
                    </a>
            
            
        </li>
    

            </ul>
            
        </li>
    


            
            <li class="divider"></li>
            <li>
                <a href="https://www.gitbook.com" target="blank" class="gitbook-link">
                    Published with GitBook
                </a>
            </li>
            
        </ul>
    </nav>
</div>

    <div class="book-body">
        <div class="body-inner">
            <div class="book-header" role="navigation">
    <!-- Actions Left -->
    

    <!-- Title -->
    <h1>
        <i class="fa fa-circle-o-notch fa-spin"></i>
        <a href="../" >欢迎学习django课程</a>
    </h1>
</div>

            <div class="page-wrapper" tabindex="-1" role="main">
                <div class="page-inner">
                
                
                    <section class="normal" id="section-">
                    
                        <h1 id="&#x4F7F;&#x7528;ajax">&#x4F7F;&#x7528;Ajax</h1>
<ul>
<li>&#x4F7F;&#x7528;&#x89C6;&#x56FE;&#x901A;&#x8FC7;&#x4E0A;&#x4E0B;&#x6587;&#x5411;&#x6A21;&#x677F;&#x4E2D;&#x4F20;&#x9012;&#x6570;&#x636E;&#xFF0C;&#x9700;&#x8981;&#x5148;&#x52A0;&#x8F7D;&#x5B8C;&#x6210;&#x6A21;&#x677F;&#x7684;&#x9759;&#x6001;&#x9875;&#x9762;&#xFF0C;&#x518D;&#x6267;&#x884C;&#x6A21;&#x578B;&#x4EE3;&#x7801;&#xFF0C;&#x751F;&#x6210;&#x6700;&#x5F20;&#x7684;html&#xFF0C;&#x8FD4;&#x56DE;&#x7ED9;&#x6D4F;&#x89C8;&#x5668;&#xFF0C;&#x8FD9;&#x4E2A;&#x8FC7;&#x7A0B;&#x5C06;&#x9875;&#x9762;&#x4E0E;&#x6570;&#x636E;&#x96C6;&#x6210;&#x5230;&#x4E86;&#x4E00;&#x8D77;&#xFF0C;&#x6269;&#x5C55;&#x6027;&#x5DEE;</li>
<li>&#x6539;&#x8FDB;&#x65B9;&#x6848;&#xFF1A;&#x901A;&#x8FC7;ajax&#x7684;&#x65B9;&#x5F0F;&#x83B7;&#x53D6;&#x6570;&#x636E;&#xFF0C;&#x901A;&#x8FC7;dom&#x64CD;&#x4F5C;&#x5C06;&#x6570;&#x636E;&#x5448;&#x73B0;&#x5230;&#x754C;&#x9762;&#x4E0A;</li>
<li>&#x63A8;&#x8350;&#x4F7F;&#x7528;&#x6846;&#x67B6;&#x7684;ajax&#x76F8;&#x5173;&#x65B9;&#x6CD5;&#xFF0C;&#x4E0D;&#x8981;&#x4F7F;&#x7528;XMLHttpRequest&#x5BF9;&#x8C61;&#xFF0C;&#x56E0;&#x4E3A;&#x64CD;&#x4F5C;&#x9EBB;&#x70E6;&#x4E14;&#x4E0D;&#x5BB9;&#x6613;&#x67E5;&#x9519;</li>
<li>jquery&#x6846;&#x67B6;&#x4E2D;&#x63D0;&#x4F9B;&#x4E86;$.ajax&#x3001;$.get&#x3001;$.post&#x65B9;&#x6CD5;&#xFF0C;&#x7528;&#x4E8E;&#x8FDB;&#x884C;&#x5F02;&#x6B65;&#x4EA4;&#x4E92;</li>
<li>&#x7531;&#x4E8E;csrf&#x7684;&#x7EA6;&#x675F;&#xFF0C;&#x63A8;&#x8350;&#x4F7F;&#x7528;$.get</li>
<li>&#x793A;&#x4F8B;&#xFF1A;&#x5B9E;&#x73B0;&#x7701;&#x5E02;&#x533A;&#x7684;&#x9009;&#x62E9;</li>
<li>&#x6700;&#x7EC8;&#x5B9E;&#x73B0;&#x6548;&#x679C;&#x5982;&#x56FE;&#xFF1A;</li>
</ul>
<p><img src="images/city.gif" alt="city"></p>
<h4 id="&#x5F15;&#x5165;js&#x6587;&#x4EF6;">&#x5F15;&#x5165;js&#x6587;&#x4EF6;</h4>
<ul>
<li>js&#x6587;&#x4EF6;&#x5C5E;&#x4E8E;&#x9759;&#x6001;&#x6587;&#x4EF6;&#xFF0C;&#x521B;&#x5EFA;&#x76EE;&#x5F55;&#x7ED3;&#x6784;&#x5982;&#x56FE;&#xFF1A;</li>
</ul>
<p><img src="images/static.png" alt="static"></p>
<h4 id="&#x4FEE;&#x6539;settingspy&#x5173;&#x4E8E;&#x9759;&#x6001;&#x6587;&#x4EF6;&#x7684;&#x8BBE;&#x7F6E;">&#x4FEE;&#x6539;settings.py&#x5173;&#x4E8E;&#x9759;&#x6001;&#x6587;&#x4EF6;&#x7684;&#x8BBE;&#x7F6E;</h4>
<pre><code>STATIC_URL = &apos;/static/&apos;
STATICFILES_DIRS = [
    os.path.join(BASE_DIR, &apos;static&apos;),
]
</code></pre><h4 id="&#x5728;modelspy&#x4E2D;&#x5B9A;&#x4E49;&#x6A21;&#x578B;">&#x5728;models.py&#x4E2D;&#x5B9A;&#x4E49;&#x6A21;&#x578B;</h4>
<pre><code>class AreaInfo(models.Model):
    aid = models.IntegerField(primary_key=True)
    atitle = models.CharField(max_length=20)
    aPArea = models.ForeignKey(&apos;AreaInfo&apos;, null=True)
</code></pre><h4 id="&#x751F;&#x6210;&#x8FC1;&#x79FB;">&#x751F;&#x6210;&#x8FC1;&#x79FB;</h4>
<pre><code>python manage.py makemigrations
python manage.py migrate
</code></pre><h4 id="&#x901A;&#x8FC7;workbench&#x5411;&#x8868;&#x4E2D;&#x586B;&#x5145;&#x793A;&#x4F8B;&#x6570;&#x636E;">&#x901A;&#x8FC7;workbench&#x5411;&#x8868;&#x4E2D;&#x586B;&#x5145;&#x793A;&#x4F8B;&#x6570;&#x636E;</h4>
<ul>
<li>&#x53C2;&#x89C1;&#x201C;&#x7701;&#x5E02;&#x533A;.sql&#x201D;</li>
<li>&#x6CE8;&#x610F;&#x5C06;&#x8868;&#x7684;&#x540D;&#x79F0;&#x5B8C;&#x6210;&#x66FF;&#x6362;</li>
</ul>
<h4 id="&#x5728;viewspy&#x4E2D;&#x7F16;&#x5199;&#x89C6;&#x56FE;">&#x5728;views.py&#x4E2D;&#x7F16;&#x5199;&#x89C6;&#x56FE;</h4>
<ul>
<li>index&#x7528;&#x4E8E;&#x5C55;&#x793A;&#x9875;&#x9762;</li>
<li>getArea1&#x7528;&#x4E8E;&#x8FD4;&#x56DE;&#x7701;&#x7EA7;&#x6570;&#x636E;</li>
<li>getArea2&#x7528;&#x4E8E;&#x6839;&#x636E;&#x7701;&#x3001;&#x5E02;&#x7F16;&#x53F7;&#x8FD4;&#x56DE;&#x5E02;&#x3001;&#x533A;&#x4FE1;&#x606F;&#xFF0C;&#x683C;&#x5F0F;&#x90FD;&#x4E3A;&#x5B57;&#x5178;&#x5BF9;&#x8C61;</li>
</ul>
<pre><code>from django.shortcuts import render
from django.http import JsonResponse
from models import AreaInfo

def index(request):
    return render(request, &apos;ct1/index.html&apos;)

def getArea1(request):
    list = AreaInfo.objects.filter(aPArea__isnull=True)
    list2 = []
    for a in list:
        list2.append([a.aid, a.atitle])
    return JsonResponse({&apos;data&apos;: list2})

def getArea2(request, pid):
    list = AreaInfo.objects.filter(aPArea_id=pid)
    list2 = []
    for a in list:
        list2.append({&apos;id&apos;: a.aid, &apos;title&apos;: a.atitle})
    return JsonResponse({&apos;data&apos;: list2})
</code></pre><h4 id="&#x5728;urlspy&#x4E2D;&#x914D;&#x7F6E;urlconf">&#x5728;urls.py&#x4E2D;&#x914D;&#x7F6E;urlconf</h4>
<pre><code>from django.conf.urls import url
from . import views

urlpatterns = [
    url(r&apos;^$&apos;, views.index),
    url(r&apos;^area1/$&apos;, views.getArea1),
    url(r&apos;^([0-9]+)/$&apos;, views.getArea2),
]
</code></pre><h4 id="&#x4E3B;urlspy&#x4E2D;&#x5305;&#x542B;&#x6B64;&#x5E94;&#x7528;&#x7684;url">&#x4E3B;urls.py&#x4E2D;&#x5305;&#x542B;&#x6B64;&#x5E94;&#x7528;&#x7684;url</h4>
<pre><code>from django.conf.urls import include, url
from django.contrib import admin

urlpatterns = [
    url(r&apos;^&apos;, include(&apos;ct1.urls&apos;, namespace=&apos;ct1&apos;)),
    url(r&apos;^admin/&apos;, include(admin.site.urls)),
]
</code></pre><h4 id="&#x5B9A;&#x4E49;&#x6A21;&#x677F;indexhtml">&#x5B9A;&#x4E49;&#x6A21;&#x677F;index.html</h4>
<ul>
<li>&#x5728;&#x9879;&#x76EE;&#x4E2D;&#x7684;&#x76EE;&#x5F55;&#x7ED3;&#x6784;&#x5982;&#x56FE;&#xFF1A;</li>
</ul>
<p><img src="images/templates.png" alt="templates"></p>
<ul>
<li>&#x4FEE;&#x6539;settings.py&#x7684;TEMPLATES&#x9879;&#xFF0C;&#x8BBE;&#x7F6E;DIRS&#x503C;</li>
</ul>
<pre><code>&apos;DIRS&apos;: [os.path.join(BASE_DIR, &apos;templates&apos;)],
</code></pre><ul>
<li>&#x5B9A;&#x4E49;&#x6A21;&#x677F;&#x6587;&#x4EF6;&#xFF1A;&#x5305;&#x542B;&#x4E09;&#x4E2A;select&#x6807;&#x7B7E;&#xFF0C;&#x5206;&#x522B;&#x5B58;&#x653E;&#x7701;&#x5E02;&#x533A;&#x7684;&#x4FE1;&#x606F;</li>
</ul>
<pre><code>&lt;!DOCTYPE html&gt;
&lt;html&gt;
&lt;head&gt;
    &lt;title&gt;&#x7701;&#x5E02;&#x533A;&#x5217;&#x8868;&lt;/title&gt;
&lt;/head&gt;
&lt;body&gt;
&lt;select id=&quot;pro&quot;&gt;
    &lt;option value=&quot;&quot;&gt;&#x8BF7;&#x9009;&#x62E9;&#x7701;&lt;/option&gt;
&lt;/select&gt;
&lt;select id=&quot;city&quot;&gt;
    &lt;option value=&quot;&quot;&gt;&#x8BF7;&#x9009;&#x62E9;&#x5E02;&lt;/option&gt;
&lt;/select&gt;
&lt;select id=&quot;dis&quot;&gt;
    &lt;option value=&quot;&quot;&gt;&#x8BF7;&#x9009;&#x62E9;&#x533A;&#x53BF;&lt;/option&gt;
&lt;/select&gt;
&lt;/body&gt;
&lt;/html&gt;
</code></pre><h4 id="&#x5728;&#x6A21;&#x677F;&#x4E2D;&#x5F15;&#x5165;jquery&#x6587;&#x4EF6;">&#x5728;&#x6A21;&#x677F;&#x4E2D;&#x5F15;&#x5165;jquery&#x6587;&#x4EF6;</h4>
<pre><code>&lt;script type=&quot;text/javascript&quot; src=&quot;static/ct1/js/jquery-1.12.4.min.js&quot;&gt;&lt;/script&gt;
</code></pre><h4 id="&#x7F16;&#x5199;js&#x4EE3;&#x7801;">&#x7F16;&#x5199;js&#x4EE3;&#x7801;</h4>
<ul>
<li>&#x7ED1;&#x5B9A;change&#x4E8B;&#x4EF6;</li>
<li>&#x53D1;&#x51FA;&#x5F02;&#x6B65;&#x8BF7;&#x6C42;</li>
<li>&#x4F7F;&#x7528;dom&#x6DFB;&#x52A0;&#x5143;&#x7D20;</li>
</ul>
<pre><code>    &lt;script type=&quot;text/javascript&quot;&gt;
        $(function(){

            $.get(&apos;area1/&apos;,function(dic) {
                pro=$(&apos;#pro&apos;)
                $.each(dic.data,function(index,item){
                    pro.append(&apos;&lt;option value=&apos;+item[0]+&apos;&gt;&apos;+item[1]+&apos;&lt;/option&gt;&apos;);
                })
            });

            $(&apos;#pro&apos;).change(function(){
                $.post($(this).val()+&apos;/&apos;,function(dic){
                    city=$(&apos;#city&apos;);
                    city.empty().append(&apos;&lt;option value=&quot;&quot;&gt;&#x8BF7;&#x9009;&#x62E9;&#x5E02;&lt;/option&gt;&apos;);
                    $.each(dic.data,function(index,item){
                        city.append(&apos;&lt;option value=&apos;+item.id+&apos;&gt;&apos;+item.title+&apos;&lt;/option&gt;&apos;);
                    })
                });
            });

            $(&apos;#city&apos;).change(function(){
                $.post($(this).val()+&apos;/&apos;,function(dic){
                    dis=$(&apos;#dis&apos;);
                    dis.empty().append(&apos;&lt;option value=&quot;&quot;&gt;&#x8BF7;&#x9009;&#x62E9;&#x533A;&#x53BF;&lt;/option&gt;&apos;);
                    $.each(dic.data,function(index,item){
                        dis.append(&apos;&lt;option value=&apos;+item.id+&apos;&gt;&apos;+item.title+&apos;&lt;/option&gt;&apos;);
                    })
                })
            });

        });
    &lt;/script&gt;
</code></pre>
                    
                    </section>
                
                
                </div>
            </div>
        </div>

        
        <a href="../part5/6.html" class="navigation navigation-prev " aria-label="Previous page: 分页"><i class="fa fa-angle-left"></i></a>
        
        
        <a href="../part5/8.html" class="navigation navigation-next " aria-label="Next page: 总结与作业"><i class="fa fa-angle-right"></i></a>
        
    </div>
</div>

        
<script src="../gitbook/app.js"></script>

    
    <script src="../gitbook/plugins/gitbook-plugin-search/lunr.min.js"></script>
    

    
    <script src="../gitbook/plugins/gitbook-plugin-search/search.js"></script>
    

    
    <script src="../gitbook/plugins/gitbook-plugin-sharing/buttons.js"></script>
    

    
    <script src="../gitbook/plugins/gitbook-plugin-fontsettings/buttons.js"></script>
    

<script>
require(["gitbook"], function(gitbook) {
    var config = {"highlight":{},"search":{"maxIndexSize":1000000},"sharing":{"facebook":true,"twitter":true,"google":false,"weibo":false,"instapaper":false,"vk":false,"all":["facebook","google","twitter","weibo","instapaper"]},"fontsettings":{"theme":"white","family":"sans","size":2}};
    gitbook.start(config);
});
</script>

        
    </body>
    
</html>
